import fs from 'fs';
import path from 'path';
import saveFile from "./保存文件工具.js"
import util from "util";

let list = []

async function mapDir(dir) {
    let files = await util.promisify(fs.readdir)(dir)

    for (let filename of files) {
        let pathname = path.join(dir, filename)
        let stats = await util.promisify(fs.stat)(pathname)

        if (stats.isDirectory()) {
            if (!pathname.includes("build") && !pathname.includes("res\\values")) {
                await mapDir(pathname)

            }

        } else if (stats.isFile()) {
            if (['.kt', '.java', '.xml'].includes(path.extname(pathname))) {
                await query(pathname)
            }
        }
    }


    console.log("list", list)
    saveFile("./data.json", list, true)
}


let query = async (filePath) => {
    let isShowTip = false

    const chineseRegex = /[\u4e00-\u9fa5]/g;
    let data = await util.promisify(fs.readFile)(filePath, 'utf8')

    // 将文件内容按行分割
    const lines = data.split('\n');

    // 遍历每一行，找出所有的中文字符
    for (let i = 0; i < lines.length; i++) {
        const line = lines[i];
        const chineseCharacters = line.match(chineseRegex);

        if (chineseCharacters) {
            if (!isShowTip) {
                console.log(`\n查找到${path.resolve(filePath)} 存在中文：`)
                isShowTip = true
            }
            // 如果这一行包含中文字符，将它们打印出来
            console.log(` ${i + 1}行 : ${chineseCharacters.join('')}`);
            list.push({
                line: i + 1,
                cn: chineseCharacters.join(''),
                en: "",
                remark: `文件位置：${path.resolve(filePath)}`
            })
        }
    }


}


mapDir('E:\\RCT\\ros-android\\mes')